<?php
namespace Admin\Controller;

use Think\Controller;
use Common\Util\UploadHandler;
use Common\Util\Page;
use Common\Controller\AdminbaseController;

class BeauticianController extends AdminbaseController {
    public function index(){
    	$Model = D('Beautician');
    	
    	$mobile = I('get.mobile');
    	$beauty = I('get.beauty');
    	$name = I('get.name');
    	$city = I('get.city');
    	
    	$sortField = I('get.sort_field');
    	$sortType = I('get.sort_type');
    	
    	$where = array();
    	$citys = $Model->field('city,province')->where($where)->group('city')->select();
    	$this->assign('citys', $citys);
    	
    	if ($mobile) {
    		$where ['b.mobile'] = $mobile;
    	}
    	
    	if ($name) {
    		$where ['b.name'] = array('like', '%'.$name.'%');
    	}
    	
    	if (isset($_GET ['city']) && $city != -1) {
    		$where ['b.city'] = $city;
    	}
    	
    	if ($beauty) {
    		$existsSql = M('beauty_parlor_employe')->alias('emp')
    			->join('LEFT JOIN '.C('DB_PREFIX').'beauty_parlor AS bp ON emp.bp_id=bp.id')
    			->where(array('emp.beautician_id=b.id', 'emp.status' => 1, 'bp.name' => array('like', '%'.$beauty.'%')))
    			->buildSql();
    		
    		$where [] = "EXISTS $existsSql";
    	}
    	
    	$order = 'b.created';
    	if ($sortField) {
    		$order = 'b.'.$sortField. ' '.$sortType;
    	}
    	
    	$count = $Model->alias('b')->where($where)->count();
    	$page = $this->page($count, 20);
    	
    	$list = $Model->field(array('b.*'))->alias('b')
    		->where($where)
    		->order($order)
    		->limit($page->firstRow.','.$page->listRows)
    	->select();

    	if ($list) {
    		$BeautyModel = D('BeautyParlor');
	    	foreach ($list as &$value) {
	    		$beautys = $BeautyModel->getBeauticianBeautys($value ['id']);
	    		if ($beautys) {
	    			$beautys = array_map(function ($var) {
	    				return $var ['name'];
	    			}, $beautys);
	    			 	$value ['beauty_name'] = implode('、', $beautys);
	    		}
	    	}
    	}
    	
    	$this->assign('Page', $page->show('Admin'));
    	
    	$this->assign('sort_type', !$sortType || $sortType == 'desc'?'asc':'desc');
    	$this->assign('list', $list);
    	$this->display();
    }
    
    public function add() {
    	if (!IS_POST) {
    		$this->assign('number', md5(microtime()));
    		$this->assign('jobTitles', M('job_title')->select());
    		$this->display();
    	} else {
    		$Model = D('Beautician');
    		if ($Model->addBeautician(I('post.'))) {
    			$operation = array();
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('添加成功', U('Admin/Beautician/index'));
    		} else {
    			$this->error($Model->getError());
    		}
    	}
    }
    
    public function checkPhone() {
    	$phone = I('get.phone');
    	$BeauticianModel = D('Beautician');
    	$exits = $BeauticianModel->where(array('mobile' => $phone))->find();
    	$this->ajaxReturn(array('status' => $exits?1:0));
    }
    
    //获取经纬度
	public function getCoordinates(){
		$arrData=CheckCoordinate();
		$this->ajaxReturn($arrData);
	}
    public function edit() {
    	
    	if (!IS_POST) {
    		$id = I ('get.id');
    		$Model = D('Beautician');
    		
    		$item = $Model->getBeauticianInfo($id);
    		$photos = D('Photo')->getTargetPhotos($id);
    		
    		$this->assign('jobTitles', M('job_title')->select());
    		if ($photos) {
    			$this->assign('photos', $photos);
    			$this->assign('number', $photos [0] ['upload_number']);
    		} else {
    			$this->assign('number', md5(microtime()));
    		}
    		
    		$experiences = D('WorkExperience')->getTargetExperiences($id);
    		$this->assign('experiences', $experiences);
    		
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$Model = D('Beautician');
    		if ($Model->updateInfo(I('post.'))) {
    			$operation = array('operation_object_id' => I('post.id'));
    			\Think\Hook::listen('operation', $operation);
    			$this->success('修改成功', U('/Admin/Beautician/index'));
    		} else {
    			$this->error($Model->getError());
    		}
    	}
    }
    
    public function jobTitleAdmin() {
    	$JobTitleModel = M('job_title');
    	
    	$this->assign('list', $JobTitleModel->where(array('status' => 1))->select());
    	$this->display();
    }
    
    public function addJobTitle() {
    	if (!IS_POST) {
    		$this->display();
    	} else {
    		$JobTitleModel = M('job_title');
    		
    		$data = array(
    			'title' => I('title'),
    			'created' => time()
    		);
    		$resutl = $JobTitleModel->add($data);
    		if ($resutl) {
    			$operation = array();
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('添加职称成功');
    		} else {
    			$this->error('添加职称失败');
    		}
    	}
    }
    
    public function editJobTitle() {
    	$JobTitleModel = M('job_title');
    	
    	if (!IS_POST) {
    		$id = I('get.id');
    		
    		$item = $JobTitleModel->where(array('id' => $id))->find();
    		$this->assign('item', $item);
    		$this->display();
    	} else {
    		$id = I('post.id');
    		$data = array(
    			'title' => I('title'),
    		);
    		$resutl = $JobTitleModel->where(array('id' => $id))->save($data);
    		if ($resutl) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			
    			$this->success('修改职称成功');
    		} else {
    			$this->error('修改职称失败');
    		}
    	}
    }
    
    public function deleteJobTitle() {
    	$JobTitleModel = M('job_title');
    	
    	if (!IS_POST) {
    		$id = I ('get.id');
    	
    		$result = $JobTitleModel->where(array('id' => $id))->save(array('status' => 0));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    	
    			$this->ajaxReturn(array('status' => 1, 'url' => U('jobTitleAdmin')));
    		} else {
    			$this->ajaxReturn(array('status' => 0));
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $JobTitleModel->where($where)->save(array('status' => 0));
    		} else {
    			$where ['id'] = array('in', implode(',',$ids));
    			$result = $JobTitleModel->where($where)->save(array('status' => 0));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $jobId) {
    			$operation = array('operation_object_id' => $jobId);
    			\Think\Hook::listen('operation', $operation);
    		}
    	
    		$this->ajaxReturn(array('status' => 1, 'url' => U('jobTitleAdmin')));
    	}
    }
    
    public function delete() {
    	$Model = D('Beautician');
    	
    	if (!IS_POST) {
    		$id = I ('get.id');
    	
    		$result = $OrderModel->where(array('id' => $id))->save(array('status' => 0));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($id);
    			$this->ajaxReturn(array('status' => 1, 'url' => U('/Admin/Beautician/index')));
    		} else {
    			$this->ajaxReturn(array('status' => 0));
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $Model->where($where)->save(array('status' => 0));
    		} else {
    			$where ['id'] = array('in', implode(',',$ids));
    			$result = $Model->where($where)->save(array('status' => 0));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $beauticianId) {
    			$operation = array('operation_object_id' => $beauticianId);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($beauticianId);
    		}
    	
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
    
    function ban() {
    	$Model = D('Beautician');
    	 
    	if (!IS_POST) {
    		$id = I ('get.id');
    		 
    		$result = $Model->where(array('id' => $id))->save(array('status' => 2));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($id);
    			if (IS_AJAX) {
    				$this->ajaxReturn(array('status' => 1, 'url' => U('/Admin/Beautician/index')));
    			} else {
    				$this->success('操作成功', U('/Admin/Beautician/index'));
    			}
    		} else {
    			if (IS_AJAX) {
    				$this->ajaxReturn(array('status' => 0));
    			} else {
    				$this->success('操作失败');
    			}
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $Model->where($where)->save(array('status' => 2));
    		} else {
    			$where ['id'] = array('in', implode(',',$ids));
    			$result = $Model->where($where)->save(array('status' => 2));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $beauticianId) {
    			$operation = array('operation_object_id' => $beauticianId);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($beauticianId);
    		}
    		 
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
    
    function cancelban() {
    	$Model = D('Beautician');
    	if (!IS_POST) {
    		$id = I ('get.id');
    		 
    		$result = $Model->where(array('id' => $id))->save(array('status' => 1));
    		if ($result) {
    			$operation = array('operation_object_id' => $id);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($id);
    			if (IS_AJAX) {
    				$this->ajaxReturn(array('status' => 1, 'url' => U('/Admin/Beautician/index')));
    			} else {
    				$this->success('操作成功', U('/Admin/Beautician/index'));
    			}
    		} else {
    			if (IS_AJAX) {
    				$this->ajaxReturn(array('status' => 0));
    			} else {
    				$this->success('操作失败');
    			}
    		}
    	} else {
    		$ids = I ('post.ids');
    		if (count($ids) == 1) {
    			$where = array('id' => $ids[0]);
    			$result = $Model->where($where)->save(array('status' => 1));
    		} else {
    			$where ['id'] = array('in', implode(',', $ids));
    			$result = $Model->where($where)->save(array('status' => 1));
    		}
    		 
    		if (!$result) {
    			$this->ajaxReturn(array('status' => 0 ));
    		}
    		 
    		foreach ($ids as $beauticianId) {
    			$operation = array('operation_object_id' => $beauticianId);
    			\Think\Hook::listen('operation', $operation);
    			$Model->clearCache($beauticianId);
    		}
    		 
    		$this->ajaxReturn(array('status' => 1, 'url' => U('index')));
    	}
    }
    
    /**
     * 获取美容师的工作时间
     */
    public function getTime() {
    	$id = I('get.id');
    	
    	$todayDate = date ( 'Y-m-d' );
    	$dayStart = strtotime ( $todayDate . ' 00:00:00' );
    	$dayEnd = strtotime ( $todayDate . ' 23:59:59' );
    	$todayStart = strtotime ( $todayDate . ' 08:00:00' );
    	$todayEnd = strtotime ( $todayDate . ' 22:00:00' );
    
    	$ScheduleModel = M('schedule');
    	$setting = $ScheduleModel->where(array('uid' => $id))->order('CurrentDate DESC')->limit(1)->find();
    	if ($setting) {
    		$setting = unserialize($setting ['workTime']);
    	}
    
    	if (!$setting) {
			$todaySetting = 0;
			$tomorrowSetting = 0;
			$afterTomorrowSetting = 0;
		} else {
			if (array_key_exists($dayStart, $setting)) {
				$todaySetting = $setting [$dayStart];
				if ($todaySetting == 0) {
					$todaySetting = array();
				}
			} else {
				$todaySetting = 0;
			}
			
			if (array_key_exists(strtotime ( '+1 day', $dayStart), $setting)) {
				$tomorrowSetting = $setting [strtotime ( '+1 day', $dayStart)];
				if ($tomorrowSetting == 0) {
					$tomorrowSetting = array();
				}
			} else {
				$tomorrowSetting = 0;
			}
			
			if (array_key_exists(strtotime ( '+2 day', $dayStart), $setting)) {
				$afterTomorrowSetting = $setting [strtotime ( '+2 day', $dayStart)];
				if ($afterTomorrowSetting == 0) {
					$afterTomorrowSetting = array();
				}
			} else {
				$afterTomorrowSetting = 0;
			}
		}
		
	
		$data = array (
			'today' => array (
				'day_start' => $dayStart,
				'day_end' => $dayEnd,
				'start' => $todayStart,
				'end' => $todayEnd,
				'setting' => $todaySetting
			),
			'tomorrow' => array (
				'day_start' => strtotime ( '+1 day',$dayStart),
				'day_end' => strtotime ( '+1 day', $dayEnd),
				'start' => strtotime ( '+1 day', $todayStart ),
				'end' => strtotime ( '+1 day', $todayEnd ),
				'setting' => $tomorrowSetting,
			),
			'afterTomorrow' => array (
				'day_start' => strtotime ( '+2 day', $dayStart),
				'day_end' => strtotime ( '+2 day', $dayEnd),
				'start' => strtotime ( '+2 day', $todayStart ),
				'end' => strtotime ( '+2 day', $todayEnd ),
				'setting' => $afterTomorrowSetting
			),
			'config_start' => $todayStart,
			'config_end' => $todayEnd
		);
    
    	$OrderModel = D('Order');
    	$orderTimes = array();
    	$orders= $OrderModel->getBeauticianOrderTimes($id, $todayStart);
    	if ($orders) {
    		foreach ($orders as $key => $order) {
    			$orderTimes [$order ['order_time']] = $order ['order_time'] + $order ['service_time'] * 60;
    		}
    	}
    	
    	$this->ajaxReturn (array('status' => 1, 'orderTimes' => $orderTimes, 'data' => array('time' => $data)));
    }
    
    public function scheduleManage() {
    	if (IS_POST) {
    		//点击上班
    		$setting = I('post.setting');
    		$beauticianId = I('post.id');
    		
    		$currentDate = strtotime(date('Y-m-d', time()));
    		$ScheduleModel = M('schedule');
    			
    		$where = array('CurrentDate' => $currentDate, 'uid' => $beauticianId);
    		$todayHasSetting = $ScheduleModel->where($where)->find();
    		if ($todayHasSetting) {
    			$data = array('workTime' => serialize($setting));
    			$result = $ScheduleModel->where($where)->save($data);
    		} else {
    			$data = array('workTime' => serialize($setting), 'CurrentDate' => $currentDate, 'uid' => $beauticianId);
    			$result = $ScheduleModel->add($data);
    		}
    			
    		if ($result) {
    			$this->ajaxReturn(array('status' => 1));
    		} else {
    			$this->ajaxReturn(array('status' => 0));
    		}
    	}else {
    		//查询上班时间
    		$uid=I('get.id');
    		$this->assign('beautician_id', $uid);
    		
    		$this->assign('today', time());
    		$this->display();
    	}
    }
    
    public function uploadProfileImg() {
    	$options = C('PROFILE_FILE_UPLODE_CONFIG');
    	$options ['upload_url'] = get_full_url().'/Uploads/Profile/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	
    	$this->ajaxReturn($result [$param_name] [0]);
    }
    
    public function uploadCertification() {
    	$num = I('post.num');
    	$options = C('CERTIFICATION_FILE_UPLODE_CONFIG');
    	$options ['upload_dir'] .= $num.'/';
    	$options ['upload_url'] = get_full_url().'/Uploads/Certification/'.$num.'/';
    	$uploadHandler = new UploadHandler($options, false);
    	$result = $uploadHandler->post(false);
    	$param_name = $options ['param_name'];
    	$this->ajaxReturn($result [$param_name]);
    }
}